#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
import re
sys.path.append('src')

from processors.docx_processor import DOCXProcessor
from core.text_cleaner import TextCleaner

def debug_text_structure():
    """调试文本结构问题"""
    
    print("=== 调试文本结构问题 ===")
    
    file_path = 'data/原始数据---选择题.docx'
    
    if not os.path.exists(file_path):
        print(f"文件不存在: {file_path}")
        return
    
    try:
        # 提取文档内容
        docx_processor = DOCXProcessor()
        with open(file_path, 'rb') as f:
            file_content = f.read()
        
        extraction_result = docx_processor.extract_content(file_content)
        raw_text = extraction_result['text']
        
        # 最小清洗
        cleaning_config = {
            'clean_unicode': False,
            'remove_special_chars': False,
            'fix_line_breaks': False,
            'remove_headers_footers': False,
            'remove_duplicates': False,
            'remove_empty_lines': False,
            'min_line_length': 0,
            'preserve_formatting': True
        }
        
        text_cleaner = TextCleaner(cleaning_config)
        cleaned_text = text_cleaner.clean_text(raw_text)
        
        print(f"原始文本长度: {len(raw_text)} 字符")
        print(f"清洗后文本长度: {len(cleaned_text)} 字符")
        
        # 显示文本的前1000字符
        print(f"\n=== 文本前1000字符 ===")
        print(repr(cleaned_text[:1000]))
        
        # 分析换行符
        print(f"\n=== 换行符分析 ===")
        print(f"包含\\n的数量: {cleaned_text.count('\n')}")
        print(f"包含\\r的数量: {cleaned_text.count('\r')}")
        print(f"包含\\r\\n的数量: {cleaned_text.count('\r\n')}")
        
        # 按不同方式分割文本
        lines_n = cleaned_text.split('\n')
        lines_rn = cleaned_text.split('\r\n')
        lines_r = cleaned_text.split('\r')
        
        print(f"按\\n分割: {len(lines_n)} 行")
        print(f"按\\r\\n分割: {len(lines_rn)} 行")
        print(f"按\\r分割: {len(lines_r)} 行")
        
        # 显示前10行
        print(f"\n=== 前10行内容（按\\n分割）===")
        for i, line in enumerate(lines_n[:10]):
            print(f"行 {i:2d}: {repr(line)}")
        
        # 查找题目模式
        print(f"\n=== 查找题目模式 ===")
        
        # 在整个文本中查找数字+点模式
        pattern = re.compile(r'\d+\.')
        matches = pattern.finditer(cleaned_text)
        
        match_positions = []
        for match in matches:
            start = match.start()
            end = match.end()
            context_start = max(0, start - 20)
            context_end = min(len(cleaned_text), end + 50)
            context = cleaned_text[context_start:context_end]
            match_positions.append((start, end, match.group(), context))
        
        print(f"找到 {len(match_positions)} 个数字+点模式")
        
        # 显示前15个匹配
        for i, (start, end, match_text, context) in enumerate(match_positions[:15]):
            print(f"匹配 {i+1:2d}: 位置 {start:4d}-{end:4d}, 内容: '{match_text}', 上下文: {repr(context)}")
        
        # 尝试按题目分割
        print(f"\n=== 尝试按题目分割 ===")
        
        # 使用正则表达式分割
        parts = re.split(r'(?=\d+\.)', cleaned_text)
        parts = [part.strip() for part in parts if part.strip()]
        
        print(f"按题目模式分割后得到 {len(parts)} 个部分")
        
        # 显示前5个部分
        for i, part in enumerate(parts[:5]):
            print(f"\n部分 {i+1}:")
            print(f"  长度: {len(part)} 字符")
            print(f"  内容: {repr(part[:100])}...")
        
        # 测试新的切片逻辑
        print(f"\n=== 测试新的切片逻辑 ===")
        
        chunks = []
        chunk_count = 0
        
        for i, part in enumerate(parts):
            if part.strip():
                # 检查是否以数字+点开头
                if re.match(r'^\d+\.', part.strip()):
                    chunk_type = 'question'
                else:
                    chunk_type = 'header'
                
                chunk = {
                    'chunk_id': f'chunk_{chunk_count:03d}',
                    'content': part.strip(),
                    'metadata': {
                        'chunk_type': chunk_type,
                        'char_count': len(part.strip()),
                        'has_answer': '答案：' in part or '答案:' in part
                    }
                }
                chunks.append(chunk)
                chunk_count += 1
                print(f"创建切片 {chunk_count}: 类型={chunk_type}, 长度={len(part.strip())}, 有答案={chunk['metadata']['has_answer']}")
        
        print(f"\n总共创建了 {len(chunks)} 个切片")
        
        # 统计
        question_chunks = [c for c in chunks if c['metadata']['chunk_type'] == 'question']
        header_chunks = [c for c in chunks if c['metadata']['chunk_type'] == 'header']
        chunks_with_answer = [c for c in chunks if c['metadata']['has_answer']]
        
        print(f"题目切片: {len(question_chunks)} 个")
        print(f"标题切片: {len(header_chunks)} 个")
        print(f"包含答案的切片: {len(chunks_with_answer)} 个")
        
    except Exception as e:
        print(f"调试过程中出错: {e}")
        import traceback
        traceback.print_exc()

if __name__ == '__main__':
    debug_text_structure()